<?php


namespace app\shared\ext;
class AnExcel
{

    /**
     * 导出
     * @param string $expTitle
     * @param array $expCellName
     * @param array $expTableData
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public static function exportExcel($expTitle = 'cache', $expCellName = [], $expTableData = [])
    {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

        $objPHPExcel = new \PHPExcel();
        // 导出
        foreach ($expCellName as $key => $cell) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$key] . 1, $cell);
        }
        foreach ($expTableData as $key => $data) {
            foreach ($data as $k => $v) {
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$k] . ($key+ 2), ' ' . $v);
            }
        }
        // 时间搓
        $time=date('YmdHis');
        $objPHPExcel->setActiveSheetIndex();
        header('Content-Type: applicationnd.ms-excel');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");
        header("Content-Disposition: attachment;filename=$fileName-$time.xlsx");
        header('Cache-Control: max-age=0');
        $objWriter->save("./excel/$fileName-$time.xlsx" );
        return "$fileName-$time.xlsx" ;
    }
}